package per.com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import per.com.bean.*;
import per.com.common.ResponseCode;
import per.com.common.ServerResponse;
import per.com.service.PcOrderService;
import per.com.service.PcTripService;
import per.com.service.PcUserOrderService;
import per.com.service.PcUsersService;
import per.com.utils.DateUtils;
import per.com.utils.UUidUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: plx
 * @Date: 2019/5/21 11:51
 * @Version 1.0
 */
@Controller
@RequestMapping("/PcOrder")
public class PcOrderController {
    @Autowired
    @Qualifier("PcOrderServiceId")
    private PcOrderService pcOrderService;

    @Autowired
    @Qualifier("PcUserOrderServiceId")
    private PcUserOrderService pcUserOrderService;

    @Autowired
    @Qualifier("PcTripServiceId")
    private PcTripService pcTripService;

    @Autowired
    @Qualifier("PcUsersServiceId")
    private PcUsersService pcUsersService;
    /**
     * 创建订单
     * @param order
     * @return
     */
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode addOrder(Order order){
        if(order != null){
            order.setId(UUidUtils.getId());
            order.setStatus(0);
            order.setCreatetime(new Date());
            pcOrderService.insertOrder(order);

            return ResponseCode.SUCCESS;
        }
       return ResponseCode.ERROR;
    }

    /**
     * 申请加入订单
     * @param orderId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/applyOrder",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode applyOrder(@RequestParam String orderId,@RequestParam String userId){
        if(orderId != null){
            Order order = pcOrderService.findOrderById(orderId);
            if(order != null){
                UserOrder userOrder = new UserOrder();
                userOrder.setId(UUidUtils.getId());
                userOrder.setCreatetime(new Date());
                userOrder.setOrderId(orderId);
                userOrder.setUserId(userId);
                pcUserOrderService.insertIntoOrderUser(userOrder);
                return ResponseCode.SUCCESS;
            }
            return ResponseCode.ERROR;
        }

        return ResponseCode.ERROR;
    }

    /**
     * 查找当前用户所有订单
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getMyOrder",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<Order>> getMyOrder(@RequestParam String userId){
        if(userId != null){
            List<Order> list = pcOrderService.getOrderByUserId(userId);
            if(list.size()>0){
                return ServerResponse.createBySuccess("查询成功",list);
            }
            return ServerResponse.createByErrorMessage("当前用户无订单信息");
        }
        return ServerResponse.createByErrorMessage("缺少必传参数");

    }

    /**
     * 查找我的订单中申请加入订单的用户信息
     * @return
     */
    @RequestMapping(value = "/getOrderMessage",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Order> getOrderMessage(@RequestParam String orderId){
        if(orderId != null){
            Order order = pcOrderService.findOrderById(orderId);
            if(order != null){
                List<UserOrder> list = pcUserOrderService.getOrderUserByOrderId(orderId);
                if(list.size()>0){
                    List<ApplyOrder>  list1 = new ArrayList<>();
                    for (UserOrder userOrder:list){
                        if(userOrder != null){
                            ApplyOrder applyOrder = new ApplyOrder();
                            applyOrder.setUserId(userOrder.getUserId());
                            applyOrder.setNote(userOrder.getNote());
                            list1.add(applyOrder);

                        }
                    }
                    order.setApplyOrder(list1);
                }
                return ServerResponse.createBySuccess("查询成功",order);
            }
        }
        return ServerResponse.createByErrorMessage("缺少必传参数");
    }

    /**
     * 同意加入行程
     * @param userId
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/agreeApply",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Trip> applyAgree(@RequestParam String userId,@RequestParam String orderId){
        Trip trip = new Trip();
        if(orderId != null && userId != null){
            Order order = pcOrderService.findOrderById(orderId);
            if(order != null){
                String driver = order.getUserId();
                if(driver != null){

                    trip.setDriver(driver);
                    trip.setId(UUidUtils.getId());
                    trip.setOrderId(orderId);
                    trip.setPassengers(userId);
                    trip.setCreatetime(new Date());
                    trip.setOrder(order);
                    int userMax = order.getUserMax();
                    int nouNum = pcTripService.orderCount(orderId);
                    if(nouNum==userMax){
                        return ServerResponse.createByErrorMessage("行程人数已满");
                    }
                    pcTripService.addPassengers(trip);
                    return ServerResponse.createBySuccess("已添加至您的行程",trip);
                }
            }
        }
       return ServerResponse.createByErrorMessage("缺少必传参数");
    }

    /**
     * 查找我的行程
     * @param userId
     * @return
     */
    @RequestMapping(value = "/myTrip",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<Trip>> getMyTrip(@RequestParam String userId){
        if(userId != null){
            List<Trip> list = pcTripService.getMyTrip(userId);
            for (Trip trip : list){
                if(trip != null){
                 String orderId  = trip.getOrderId();
                 Order order = pcOrderService.findOrderById(orderId);
                 String driverId = trip.getDriver();
                 String passId = trip.getPassengers();
                 User driver = pcUsersService.getUserById(driverId);
                 User passenger = pcUsersService.getUserById(passId);
                 trip.setDriverinfo(driver);
                 trip.setPassengesinfo(passenger);
                 trip.setOrder(order);
                }
            }
            return ServerResponse.createBySuccess("查询成功",list);
        }
        return ServerResponse.createByErrorMessage("缺少必传参数");
    }

    /**
     * 用户退出行程
     * @param passengers
     * @return
     */
    @RequestMapping(value = "/exitTrip",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode exitTrip(@RequestParam String passengers,@RequestParam String orderId){
        if(passengers != null&& orderId != null){
            pcTripService.exitTrip(passengers,orderId);
            return ResponseCode.SUCCESS;
        }
        return ResponseCode.ERROR;
    }

    /**
     * 用户取消订单
     * @param userId
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteOrder",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteOrder(@RequestParam String userId,@RequestParam String id){
        if(userId != null && id != null){
            int count  = pcTripService.orderCount(id);
            if (count != 0) {
                return ServerResponse.createByErrorMessage("该订单已存在于行程中，请联系乘客退出行程再取消订单");

            }
                pcOrderService.deleteOrder(id,userId);
                return ServerResponse.createBySuccess("删除成功");
        }
        return ServerResponse.createByErrorMessage("缺少必传参数");
    }
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    @ResponseBody
    public ResponseCode get(){
        return ResponseCode.SUCCESS;
    }
}
